home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources / cexp.y < prev    next >
Encoding:
Text File  |  1993-03-10  |  22.9 KB  |  1,020 lines  |  [TEXT/MPS ]

  1. /* Parse C expressions for CCCP.
  2.    Copyright (C) 1987, 1992 Free Software Foundation.
  3.  
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  In other words, you are welcome to use, share and improve this program.
  19.  You are forbidden to forbid anyone else to use, share and improve
  20.  what you give them.   Help stamp out software-hoarding!
  21.  
  22.  Adapted from expread.y of GDB by Paul Rubin, July 1986.  */
  23.  
  24. /* Parse a C expression from text in a string  */
  25.    
  26. %{
  27. #ifdef MPW
  28. #include <stdlib.h>
  29. #endif
  30. #include "config.h"
  31. #include <setjmp.h>
  32. /* #define YYDEBUG 1 */
  33.  
  34. #ifdef MULTIBYTE_CHARS
  35. #ifndef MPW
  36. #include <stdlib.h>
  37. #endif
  38. #include <locale.h>
  39. #endif
  40.  
  41. #include <stdio.h>
  42.  
  43. typedef unsigned char U_CHAR;
  44.  
  45. /* This is used for communicating lists of keywords with cccp.c.  */
  46. struct arglist {
  47.   struct arglist *next;
  48.   U_CHAR *name;
  49.   int length;
  50.   int argno;
  51. };
  52.  
  53. /* Define a generic NULL if one hasn't already been defined.  */
  54.  
  55. #ifndef NULL
  56. #define NULL 0
  57. #endif
  58.  
  59. #ifndef GENERIC_PTR
  60. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  61. #define GENERIC_PTR void *
  62. #else
  63. #define GENERIC_PTR char *
  64. #endif
  65. #endif
  66.  
  67. #ifndef NULL_PTR
  68. #define NULL_PTR ((GENERIC_PTR)0)
  69. #endif
  70.  
  71. int yylex ();
  72. void yyerror ();
  73. int expression_value;
  74.  
  75. static jmp_buf parse_return_error;
  76.  
  77. /* Nonzero means count most punctuation as part of a name.  */
  78. static int keyword_parsing = 0;
  79.  
  80. /* some external tables of character types */
  81. extern unsigned char is_idstart[], is_idchar[], is_hor_space[];
  82.  
  83. extern char *xmalloc ();
  84.  
  85. /* Flag for -pedantic.  */
  86. extern int pedantic;
  87.  
  88. /* Flag for -traditional.  */
  89. extern int traditional;
  90.  
  91. #ifndef CHAR_TYPE_SIZE
  92. #define CHAR_TYPE_SIZE BITS_PER_UNIT
  93. #endif
  94.  
  95. #ifndef INT_TYPE_SIZE
  96. #define INT_TYPE_SIZE BITS_PER_WORD
  97. #endif
  98.  
  99. #ifndef LONG_TYPE_SIZE
  100. #define LONG_TYPE_SIZE BITS_PER_WORD
  101. #endif
  102.  
  103. #ifndef WCHAR_TYPE_SIZE
  104. #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
  105. #endif
  106.  
  107. /* Yield nonzero if adding two numbers with A's and B's signs can yield a
  108.    number with SUM's sign, where A, B, and SUM are all C integers.  */
  109. #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
  110.  
  111. static void integer_overflow ();
  112. static long left_shift ();
  113. static long right_shift ();
  114. %}
  115.  
  116. %union {
  117.   struct constant {long value; int unsignedp;} integer;
  118.   struct name {U_CHAR *address; int length;} name;
  119.   struct arglist *keywords;
  120.   int voidval;
  121.   char *sval;
  122. }
  123.  
  124. %type <integer> exp exp1 start
  125. %type <keywords> keywords
  126. %token <integer> INT CHAR
  127. %token <name> NAME
  128. %token <integer> ERROR
  129.  
  130. %right '?' ':'
  131. %left ','
  132. %left OR
  133. %left AND
  134. %left '|'
  135. %left '^'
  136. %left '&'
  137. %left EQUAL NOTEQUAL
  138. %left '<' '>' LEQ GEQ
  139. %left LSH RSH
  140. %left '+' '-'
  141. %left '*' '/' '%'
  142. %right UNARY
  143.  
  144. /* %expect 40 */
  145.  
  146. %%
  147.  
  148. start   :    exp1
  149.         { expression_value = $1.value; }
  150.     ;
  151.  
  152. /* Expressions, including the comma operator.  */
  153. exp1    :    exp
  154.     |    exp1 ',' exp
  155.             { if (pedantic)
  156.                 pedwarn ("comma operator in operand of `#if'");
  157.               $$ = $3; }
  158.     ;
  159.  
  160. /* Expressions, not including the comma operator.  */
  161. exp    :    '-' exp    %prec UNARY
  162.             { $$.value = - $2.value;
  163.               if (($$.value & $2.value) < 0 && ! $2.unsignedp)
  164.                 integer_overflow ();
  165.               $$.unsignedp = $2.unsignedp; }
  166.     |    '!' exp    %prec UNARY
  167.             { $$.value = ! $2.value;
  168.               $$.unsignedp = 0; }
  169.     |    '+' exp    %prec UNARY
  170.             { $$ = $2; }
  171.     |    '~' exp    %prec UNARY
  172.             { $$.value = ~ $2.value;
  173.               $$.unsignedp = $2.unsignedp; }
  174.     |    '#' NAME
  175.               { $$.value = check_assertion ($2.address, $2.length,
  176.                               0, NULL_PTR);
  177.               $$.unsignedp = 0; }
  178.     |    '#' NAME
  179.             { keyword_parsing = 1; }
  180.         '(' keywords ')'
  181.               { $$.value = check_assertion ($2.address, $2.length,
  182.                               1, $5);
  183.               keyword_parsing = 0;
  184.               $$.unsignedp = 0; }
  185.     |    '(' exp1 ')'
  186.             { $$ = $2; }
  187.     ;
  188.  
  189. /* Binary operators in order of decreasing precedence.  */
  190. exp    :    exp '*' exp
  191.             { $$.unsignedp = $1.unsignedp || $3.unsignedp;
  192.               if ($$.unsignedp)
  193.                 $$.value = (unsigned long) $1.value * $3.value;
  194.               else
  195.                 {
  196.                   $$.value = $1.value * $3.value;
  197.                   if ($1.value
  198.                   && ($$.value / $1.value != $3.value
  199.                       || ($$.value & $1.value & $3.value) < 0))
  200.                 integer_overflow ();
  201.                 } }
  202.     |    exp '/' exp
  203.             { if ($3.value == 0)
  204.                 {
  205.                   error ("division by zero in #if");
  206.                   $3.value = 1;
  207.                 }
  208.               $$.unsignedp = $1.unsignedp || $3.unsignedp;
  209.               if ($$.unsignedp)
  210.                 $$.value = (unsigned long) $1.value / $3.value;
  211.               else
  212.                 {
  213.                   $$.value = $1.value / $3.value;
  214.                   if (($$.value & $1.value & $3.value) < 0)
  215.                 integer_overflow ();
  216.                 } }
  217.     |    exp '%' exp
  218.             { if ($3.value == 0)
  219.                 {
  220.                   error ("division by zero in #if");
  221.                   $3.value = 1;
  222.                 }
  223.               $$.unsignedp = $1.unsignedp || $3.unsignedp;
  224.               if ($$.unsignedp)
  225.                 $$.value = (unsigned long) $1.value % $3.value;
  226.               else
  227.                 $$.value = $1.value % $3.value; }
  228.     |    exp '+' exp
  229.             { $$.value = $1.value + $3.value;
  230.               $$.unsignedp = $1.unsignedp || $3.unsignedp;
  231.               if (! $$.unsignedp
  232.                   && ! possible_sum_sign ($1.value, $3.value,
  233.                               $$.value))
  234.                 integer_overflow (); }
  235.     |    exp '-' exp
  236.             { $$.value = $1.value - $3.value;
  237.               $$.unsignedp = $1.unsignedp || $3.unsignedp;
  238.               if (! $$.unsignedp
  239.                   && ! possible_sum_sign ($$.value, $3.value,
  240.                               $1.value))
  241.                 integer_overflow (); }
  242.     |    exp LSH exp
  243.             { $$.unsignedp = $1.unsignedp;
  244.               if ($3.value < 0 && ! $3.unsignedp)
  245.                 $$.value = right_shift (&$1, -$3.value);
  246.               else
  247.                 $$.value = left_shift (&$1, $3.value); }
  248.     |    exp RSH exp
  249.             { $$.unsignedp = $1.unsignedp;
  250.               if ($3.value < 0 && ! $3.unsignedp)
  251.                 $$.value = left_shift (&$1, -$3.value);
  252.               else
  253.                 $$.value = right_shift (&$1, $3.value); }
  254.     |    exp EQUAL exp
  255.             { $$.value = ($1.value == $3.value);
  256.               $$.unsignedp = 0; }
  257.     |    exp NOTEQUAL exp
  258.             { $$.value = ($1.value != $3.value);
  259.               $$.unsignedp = 0; }
  260.     |    exp LEQ exp
  261.             { $$.unsignedp = 0;
  262.               if ($1.unsignedp || $3.unsignedp)
  263.                 $$.value = (unsigned long) $1.value <= $3.value;
  264.               else
  265.                 $$.value = $1.value <= $3.value; }
  266.     |    exp GEQ exp
  267.             { $$.unsignedp = 0;
  268.               if ($1.unsignedp || $3.unsignedp)
  269.                 $$.value = (unsigned long) $1.value >= $3.value;
  270.               else
  271.                 $$.value = $1.value >= $3.value; }
  272.     |    exp '<' exp
  273.             { $$.unsignedp = 0;
  274.               if ($1.unsignedp || $3.unsignedp)
  275.                 $$.value = (unsigned long) $1.value < $3.value;
  276.               else
  277.                 $$.value = $1.value < $3.value; }
  278.     |    exp '>' exp
  279.             { $$.unsignedp = 0;
  280.               if ($1.unsignedp || $3.unsignedp)
  281.                 $$.value = (unsigned long) $1.value > $3.value;
  282.               else
  283.                 $$.value = $1.value > $3.value; }
  284.     |    exp '&' exp
  285.             { $$.value = $1.value & $3.value;
  286.               $$.unsignedp = $1.unsignedp || $3.unsignedp; }
  287.     |    exp '^' exp
  288.             { $$.value = $1.value ^ $3.value;
  289.               $$.unsignedp = $1.unsignedp || $3.unsignedp; }
  290.     |    exp '|' exp
  291.             { $$.value = $1.value | $3.value;
  292.               $$.unsignedp = $1.unsignedp || $3.unsignedp; }
  293.     |    exp AND exp
  294.             { $$.value = ($1.value && $3.value);
  295.               $$.unsignedp = 0; }
  296.     |    exp OR exp
  297.             { $$.value = ($1.value || $3.value);
  298.               $$.unsignedp = 0; }
  299.     |    exp '?' exp ':' exp
  300.             { $$.value = $1.value ? $3.value : $5.value;
  301.               $$.unsignedp = $3.unsignedp || $5.unsignedp; }
  302.     |    INT
  303.             { $$ = yylval.integer; }
  304.     |    CHAR
  305.             { $$ = yylval.integer; }
  306.     |    NAME
  307.             { $$.value = 0;
  308.               $$.unsignedp = 0; }
  309.     ;
  310.  
  311. keywords :
  312.             { $$ = 0; } 
  313.     |    '(' keywords ')' keywords
  314.             { struct arglist *temp;
  315.               $$ = (struct arglist *) xmalloc (sizeof (struct arglist));
  316.               $$->next = $2;
  317.               $$->name = (U_CHAR *) "(";
  318.               $$->length = 1;
  319.               temp = $$;
  320.               while (temp != 0 && temp->next != 0)
  321.                 temp = temp->next;
  322.               temp->next = (struct arglist *) xmalloc (sizeof (struct arglist));
  323.               temp->next->next = $4;
  324.               temp->next->name = (U_CHAR *) ")";
  325.               temp->next->length = 1; }
  326.     |    NAME keywords
  327.             { $$ = (struct arglist *) xmalloc (sizeof (struct arglist));
  328.               $$->name = $1.address;
  329.               $$->length = $1.length;
  330.               $$->next = $2; } 
  331.     ;
  332. %%
  333.  
  334. /* During parsing of a C expression, the pointer to the next character
  335.    is in this variable.  */
  336.  
  337. static char *lexptr;
  338.  
  339. /* Take care of parsing a number (anything that starts with a digit).
  340.    Set yylval and return the token type; update lexptr.
  341.    LEN is the number of characters in it.  */
  342.  
  343. /* maybe needs to actually deal with floating point numbers */
  344.  
  345. int
  346. parse_number (olen)
  347.      int olen;
  348. {
  349.   register char *p = lexptr;
  350.   register int c;
  351.   register unsigned long n = 0, nd, ULONG_MAX_over_base;
  352.   register int base = 10;
  353.   register int len = olen;
  354.   register int overflow = 0;
  355.   register int digit, largest_digit = 0;
  356.   int spec_long = 0;
  357.  
  358.   for (c = 0; c < len; c++)
  359.     if (p[c] == '.') {
  360.       /* It's a float since it contains a point.  */
  361.       yyerror ("floating point numbers not allowed in #if expressions");
  362.       return ERROR;
  363.     }
  364.  
  365.   yylval.integer.unsignedp = 0;
  366.  
  367.   if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
  368.     p += 2;
  369.     base = 16;
  370.     len -= 2;
  371.   }
  372.   else if (*p == '0')
  373.     base = 8;
  374.  
  375. #ifdef MPW_C
  376.   ULONG_MAX_over_base = (unsigned long) 0xffffffff / (unsigned long) base;
  377. #else
  378.   ULONG_MAX_over_base = (unsigned long) -1 / base;
  379. #endif
  380.  
  381.   for (; len > 0; len--) {
  382.     c = *p++;
  383.  
  384.     if (c >= '0' && c <= '9')
  385.       digit = c - '0';
  386.     else if (base == 16 && c >= 'a' && c <= 'f')
  387.       digit = c - 'a' + 10;
  388.     else if (base == 16 && c >= 'A' && c <= 'F')
  389.       digit = c - 'A' + 10;
  390.     else {
  391.       /* `l' means long, and `u' means unsigned.  */
  392.       while (1) {
  393.     if (c == 'l' || c == 'L')
  394.       {
  395.         if (spec_long)
  396.           yyerror ("two `l's in integer constant");
  397.         spec_long = 1;
  398.       }
  399.     else if (c == 'u' || c == 'U')
  400.       {
  401.         if (yylval.integer.unsignedp)
  402.           yyerror ("two `u's in integer constant");
  403.         yylval.integer.unsignedp = 1;
  404.       }
  405.     else
  406.       break;
  407.  
  408.     if (--len == 0)
  409.       break;
  410.     c = *p++;
  411.       }
  412.       /* Don't look for any more digits after the suffixes.  */
  413.       break;
  414.     }
  415.     if (largest_digit < digit)
  416.       largest_digit = digit;
  417.     nd = n * base + digit;
  418.     overflow |= ULONG_MAX_over_base < n | nd < n;
  419.     n = nd;
  420.   }
  421.  
  422.   if (len != 0) {
  423.     yyerror ("Invalid number in #if expression");
  424.     return ERROR;
  425.   }
  426.  
  427.   if (base <= largest_digit)
  428.     warning ("integer constant contains digits beyond the radix");
  429.  
  430.   if (overflow)
  431.     warning ("integer constant out of range");
  432.   
  433.   /* If too big to be signed, consider it unsigned.  */
  434.   if ((long) n < 0 && ! yylval.integer.unsignedp)
  435.     {
  436.       if (base == 10)
  437.     warning ("integer constant is so large that it is unsigned");
  438.       yylval.integer.unsignedp = 1;
  439.     }
  440.  
  441.   lexptr = p;
  442.   yylval.integer.value = n;
  443.   return INT;
  444. }
  445.  
  446. struct token {
  447.   char *operator;
  448.   int token;
  449. };
  450.  
  451. static struct token tokentab2[] = {
  452.   {"&&", AND},
  453.   {"||", OR},
  454.   {"<<", LSH},
  455.   {">>", RSH},
  456.   {"==", EQUAL},
  457.   {"!=", NOTEQUAL},
  458.   {"<=", LEQ},
  459.   {">=", GEQ},
  460.   {"++", ERROR},
  461.   {"--", ERROR},
  462.   {NULL, ERROR}
  463. };
  464.  
  465. /* Read one token, getting characters through lexptr.  */
  466.  
  467. int
  468. yylex ()
  469. {
  470.   register int c;
  471.   register int namelen;
  472.   register char *tokstart;
  473.   register struct token *toktab;
  474.   int wide_flag;
  475.  
  476.  retry:
  477.  
  478.   tokstart = lexptr;
  479.   c = *tokstart;
  480.   /* See if it is a special token of length 2.  */
  481.   if (! keyword_parsing)
  482.     for (toktab = tokentab2; toktab->operator != NULL; toktab++)
  483.       if (c == *toktab->operator && tokstart[1] == toktab->operator[1]) {
  484.     lexptr += 2;
  485.     if (toktab->token == ERROR)
  486.       {
  487.         char *buf = (char *) alloca (40);
  488.         sprintf (buf, "`%s' not allowed in operand of `#if'", toktab->operator);
  489.         yyerror (buf);
  490.       }
  491.     return toktab->token;
  492.       }
  493.  
  494.   switch (c) {
  495.   case 0:
  496.     return 0;
  497.     
  498.   case ' ':
  499.   case '\t':
  500.   case '\r':
  501.   case '\n':
  502.     lexptr++;
  503.     goto retry;
  504.     
  505.   case 'L':
  506.     /* Capital L may start a wide-string or wide-character constant.  */
  507.     if (lexptr[1] == '\'')
  508.       {
  509.     lexptr++;
  510.     wide_flag = 1;
  511.     goto char_constant;
  512.       }
  513.     if (lexptr[1] == '"')
  514.       {
  515.     lexptr++;
  516.     wide_flag = 1;
  517.     goto string_constant;
  518.       }
  519.     break;
  520.  
  521.   case '\'':
  522.     wide_flag = 0;
  523.   char_constant:
  524.     lexptr++;
  525.     if (keyword_parsing) {
  526.       char *start_ptr = lexptr - 1;
  527.       while (1) {
  528.     c = *lexptr++;
  529.     if (c == '\\')
  530.       c = parse_escape (&lexptr);
  531.     else if (c == '\'')
  532.       break;
  533.       }
  534.       yylval.name.address = (U_CHAR *) tokstart;
  535.       yylval.name.length = lexptr - start_ptr;
  536.       return NAME;
  537.     }
  538.  
  539.     /* This code for reading a character constant
  540.        handles multicharacter constants and wide characters.
  541.        It is mostly copied from c-lex.c.  */
  542.     {
  543.       register int result = 0;
  544.       register num_chars = 0;
  545.       unsigned width = CHAR_TYPE_SIZE;
  546.       int max_chars;
  547.       char *token_buffer;
  548.  
  549.       if (wide_flag)
  550.     {
  551.       width = WCHAR_TYPE_SIZE;
  552. #ifdef MULTIBYTE_CHARS
  553.       max_chars = MB_CUR_MAX;
  554. #else
  555.       max_chars = 1;
  556. #endif
  557.     }
  558.       else
  559.     max_chars = LONG_TYPE_SIZE / width;
  560.  
  561.       token_buffer = (char *) alloca (max_chars + 1);
  562.  
  563.       while (1)
  564.     {
  565.       c = *lexptr++;
  566.  
  567.       if (c == '\'' || c == EOF)
  568.         break;
  569.  
  570.       if (c == '\\')
  571.         {
  572.           c = parse_escape (&lexptr);
  573.           if (width < HOST_BITS_PER_INT
  574.           && (unsigned) c >= (1 << width))
  575.         pedwarn ("escape sequence out of range for character");
  576.         }
  577.  
  578.       num_chars++;
  579.  
  580.       /* Merge character into result; ignore excess chars.  */
  581.       if (num_chars < max_chars + 1)
  582.         {
  583.           if (width < HOST_BITS_PER_INT)
  584.         result = (result << width) | (c & ((1 << width) - 1));
  585.           else
  586.         result = c;
  587.           token_buffer[num_chars - 1] = c;
  588.         }
  589.     }
  590.  
  591.       token_buffer[num_chars] = 0;
  592.  
  593.       if (c != '\'')
  594.     error ("malformatted character constant");
  595.       else if (num_chars == 0)
  596.     error ("empty character constant");
  597.       else if (num_chars > max_chars)
  598.     {
  599.       num_chars = max_chars;
  600.       error ("character constant too long");
  601.     }
  602.       else if (num_chars != 1 && ! traditional)
  603.     warning ("multi-character character constant");
  604.  
  605.       /* If char type is signed, sign-extend the constant.  */
  606.       if (! wide_flag)
  607.     {
  608.       int num_bits = num_chars * width;
  609.  
  610.       if (lookup ("__CHAR_UNSIGNED__", sizeof ("__CHAR_UNSIGNED__")-1, -1)
  611.           || ((result >> (num_bits - 1)) & 1) == 0)
  612.         yylval.integer.value
  613.           = result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
  614.       else
  615.         yylval.integer.value
  616.           = result | ~((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
  617.     }
  618.       else
  619.     {
  620. #ifdef MULTIBYTE_CHARS
  621.       /* Set the initial shift state and convert the next sequence.  */
  622.       result = 0;
  623.       /* In all locales L'\0' is zero and mbtowc will return zero,
  624.          so don't use it.  */
  625.       if (num_chars > 1
  626.           || (num_chars == 1 && token_buffer[0] != '\0'))
  627.         {
  628.           wchar_t wc;
  629.           (void) mbtowc (NULL_PTR, NULL_PTR, 0);
  630.           if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
  631.         result = wc;
  632.           else
  633.         warning ("Ignoring invalid multibyte character");
  634.         }
  635. #endif
  636.       yylval.integer.value = result;
  637.     }
  638.     }
  639.  
  640.     /* This is always a signed type.  */
  641.     yylval.integer.unsignedp = 0;
  642.     
  643.     return CHAR;
  644.  
  645.     /* some of these chars are invalid in constant expressions;
  646.        maybe do something about them later */
  647.   case '/':
  648.   case '+':
  649.   case '-':
  650.   case '*':
  651.   case '%':
  652.   case '|':
  653.   case '&':
  654.   case '^':
  655.   case '~':
  656.   case '!':
  657.   case '@':
  658.   case '<':
  659.   case '>':
  660.   case '[':
  661.   case ']':
  662.   case '.':
  663.   case '?':
  664.   case ':':
  665.   case '=':
  666.   case '{':
  667.   case '}':
  668.   case ',':
  669.   case '#':
  670.     if (keyword_parsing)
  671.       break;
  672.   case '(':
  673.   case ')':
  674.     lexptr++;
  675.     return c;
  676.  
  677.   case '"':
  678.   string_constant:
  679.     if (keyword_parsing) {
  680.       char *start_ptr = lexptr;
  681.       lexptr++;
  682.       while (1) {
  683.     c = *lexptr++;
  684.     if (c == '\\')
  685.       c = parse_escape (&lexptr);
  686.     else if (c == '"')
  687.       break;
  688.       }
  689.       yylval.name.address = (U_CHAR *) tokstart;
  690.       yylval.name.length = lexptr - start_ptr;
  691.       return NAME;
  692.     }
  693.     yyerror ("string constants not allowed in #if expressions");
  694.     return ERROR;
  695.   }
  696.  
  697.   if (c >= '0' && c <= '9' && !keyword_parsing) {
  698.     /* It's a number */
  699.     for (namelen = 0;
  700.      c = tokstart[namelen], is_idchar[c] || c == '.'; 
  701.      namelen++)
  702.       ;
  703.     return parse_number (namelen);
  704.   }
  705.  
  706.   /* It is a name.  See how long it is.  */
  707.  
  708.   if (keyword_parsing) {
  709.     for (namelen = 0;; namelen++) {
  710.       if (is_hor_space[tokstart[namelen]])
  711.     break;
  712.       if (tokstart[namelen] == '(' || tokstart[namelen] == ')')
  713.     break;
  714.       if (tokstart[namelen] == '"' || tokstart[namelen] == '\'')
  715.     break;
  716.     }
  717.   } else {
  718.     if (!is_idstart[c]) {
  719.       yyerror ("Invalid token in expression");
  720.       return ERROR;
  721.     }
  722.  
  723.     for (namelen = 0; is_idchar[tokstart[namelen]]; namelen++)
  724.       ;
  725.   }
  726.   
  727.   lexptr += namelen;
  728.   yylval.name.address = (U_CHAR *) tokstart;
  729.   yylval.name.length = namelen;
  730.   return NAME;
  731. }
  732.  
  733.  
  734. /* Parse a C escape sequence.  STRING_PTR points to a variable
  735.    containing a pointer to the string to parse.  That pointer
  736.    is updated past the characters we use.  The value of the
  737.    escape sequence is returned.
  738.  
  739.    A negative value means the sequence \ newline was seen,
  740.    which is supposed to be equivalent to nothing at all.
  741.  
  742.    If \ is followed by a null character, we return a negative
  743.    value and leave the string pointer pointing at the null character.
  744.  
  745.    If \ is followed by 000, we return 0 and leave the string pointer
  746.    after the zeros.  A value of 0 does not mean end of string.  */
  747.  
  748. int
  749. parse_escape (string_ptr)
  750.      char **string_ptr;
  751. {
  752.   register int c = *(*string_ptr)++;
  753.   switch (c)
  754.     {
  755.     case 'a':
  756.       return TARGET_BELL;
  757.     case 'b':
  758.       return TARGET_BS;
  759.     case 'e':
  760.       return 033;
  761.     case 'f':
  762.       return TARGET_FF;
  763.     case 'n':
  764.       return TARGET_NEWLINE;
  765.     case 'r':
  766.       return TARGET_CR;
  767.     case 't':
  768.       return TARGET_TAB;
  769.     case 'v':
  770.       return TARGET_VT;
  771.     case '\n':
  772.       return -2;
  773.     case 0:
  774.       (*string_ptr)--;
  775.       return 0;
  776.     case '^':
  777.       c = *(*string_ptr)++;
  778.       if (c == '\\')
  779.     c = parse_escape (string_ptr);
  780.       if (c == '?')
  781.     return 0177;
  782.       return (c & 0200) | (c & 037);
  783.       
  784.     case '0':
  785.     case '1':
  786.     case '2':
  787.     case '3':
  788.     case '4':
  789.     case '5':
  790.     case '6':
  791.     case '7':
  792.       {
  793.     register int i = c - '0';
  794.     register int count = 0;
  795.     while (++count < 3)
  796.       {
  797.         c = *(*string_ptr)++;
  798.         if (c >= '0' && c <= '7')
  799.           i = (i << 3) + c - '0';
  800.         else
  801.           {
  802.         (*string_ptr)--;
  803.         break;
  804.           }
  805.       }
  806.     if ((i & ~((1 << CHAR_TYPE_SIZE) - 1)) != 0)
  807.       {
  808.         i &= (1 << CHAR_TYPE_SIZE) - 1;
  809.         warning ("octal character constant does not fit in a byte");
  810.       }
  811.     return i;
  812.       }
  813.     case 'x':
  814.       {
  815.     register unsigned i = 0, overflow = 0, digits_found = 0, digit;
  816.     for (;;)
  817.       {
  818.         c = *(*string_ptr)++;
  819.         if (c >= '0' && c <= '9')
  820.           digit = c - '0';
  821.         else if (c >= 'a' && c <= 'f')
  822.           digit = c - 'a' + 10;
  823.         else if (c >= 'A' && c <= 'F')
  824.           digit = c - 'A' + 10;
  825.         else
  826.           {
  827.         (*string_ptr)--;
  828.         break;
  829.           }
  830.         overflow |= i ^ (i << 4 >> 4);
  831.         i = (i << 4) + digit;
  832.         digits_found = 1;
  833.       }
  834.     if (!digits_found)
  835.       yyerror ("\\x used with no following hex digits");
  836.     if (overflow | (i & ~((1 << BITS_PER_UNIT) - 1)))
  837.       {
  838.         i &= (1 << BITS_PER_UNIT) - 1;
  839.         warning ("hex character constant does not fit in a byte");
  840.       }
  841.     return i;
  842.       }
  843.     default:
  844.       return c;
  845.     }
  846. }
  847.  
  848. void
  849. yyerror (s)
  850.      char *s;
  851. {
  852.   error (s);
  853.   longjmp (parse_return_error, 1);
  854. }
  855.  
  856. static void
  857. integer_overflow ()
  858. {
  859.   if (pedantic)
  860.     pedwarn ("integer overflow in preprocessor expression");
  861. }
  862.  
  863. static long
  864. left_shift (a, b)
  865.      struct constant *a;
  866.      unsigned long b;
  867. {
  868.   if (b >= HOST_BITS_PER_LONG)
  869.     {
  870.       if (! a->unsignedp && a->value != 0)
  871.     integer_overflow ();
  872.       return 0;
  873.     }
  874.   else if (a->unsignedp)
  875.     return (unsigned long) a->value << b;
  876.   else
  877.     {
  878.       long l = a->value << b;
  879.       if (l >> b != a->value)
  880.     integer_overflow ();
  881.       return l;
  882.     }
  883. }
  884.  
  885. static long
  886. right_shift (a, b)
  887.      struct constant *a;
  888.      unsigned long b;
  889. {
  890.   if (b >= HOST_BITS_PER_LONG)
  891.     return a->unsignedp ? 0 : a->value >> (HOST_BITS_PER_LONG - 1);
  892.   else if (a->unsignedp)
  893.     return (unsigned long) a->value >> b;
  894.   else
  895.     return a->value >> b;
  896. }
  897.  
  898. /* This page contains the entry point to this file.  */
  899.  
  900. /* Parse STRING as an expression, and complain if this fails
  901.    to use up all of the contents of STRING.  */
  902. /* We do not support C comments.  They should be removed before
  903.    this function is called.  */
  904.  
  905. int
  906. parse_c_expression (string)
  907.      char *string;
  908. {
  909.   lexptr = string;
  910.   
  911.   if (lexptr == 0 || *lexptr == 0) {
  912.     error ("empty #if expression");
  913.     return 0;            /* don't include the #if group */
  914.   }
  915.  
  916.   /* if there is some sort of scanning error, just return 0 and assume
  917.      the parsing routine has printed an error message somewhere.
  918.      there is surely a better thing to do than this.     */
  919.   if (setjmp (parse_return_error))
  920.     return 0;
  921.  
  922.   if (yyparse ())
  923.     return 0;            /* actually this is never reached
  924.                    the way things stand. */
  925.   if (*lexptr)
  926.     error ("Junk after end of expression.");
  927.  
  928.   return expression_value;    /* set by yyparse () */
  929. }
  930.  
  931. #ifdef TEST_EXP_READER
  932. extern int yydebug;
  933.  
  934. /* Main program for testing purposes.  */
  935. int
  936. main ()
  937. {
  938.   int n, c;
  939.   char buf[1024];
  940.  
  941. /*
  942.   yydebug = 1;
  943. */
  944.   initialize_random_junk ();
  945.  
  946.   for (;;) {
  947.     printf ("enter expression: ");
  948.     n = 0;
  949.     while ((buf[n] = getchar ()) != '\n' && buf[n] != EOF)
  950.       n++;
  951.     if (buf[n] == EOF)
  952.       break;
  953.     buf[n] = '\0';
  954.     printf ("parser returned %d\n", parse_c_expression (buf));
  955.   }
  956.  
  957.   return 0;
  958. }
  959.  
  960. /* table to tell if char can be part of a C identifier. */
  961. unsigned char is_idchar[256];
  962. /* table to tell if char can be first char of a c identifier. */
  963. unsigned char is_idstart[256];
  964. /* table to tell if c is horizontal space.  isspace () thinks that
  965.    newline is space; this is not a good idea for this program. */
  966. char is_hor_space[256];
  967.  
  968. /*
  969.  * initialize random junk in the hash table and maybe other places
  970.  */
  971. initialize_random_junk ()
  972. {
  973.   register int i;
  974.  
  975.   /*
  976.    * Set up is_idchar and is_idstart tables.  These should be
  977.    * faster than saying (is_alpha (c) || c == '_'), etc.
  978.    * Must do set up these things before calling any routines tthat
  979.    * refer to them.
  980.    */
  981.   for (i = 'a'; i <= 'z'; i++) {
  982.     ++is_idchar[i - 'a' + 'A'];
  983.     ++is_idchar[i];
  984.     ++is_idstart[i - 'a' + 'A'];
  985.     ++is_idstart[i];
  986.   }
  987.   for (i = '0'; i <= '9'; i++)
  988.     ++is_idchar[i];
  989.   ++is_idchar['_'];
  990.   ++is_idstart['_'];
  991. #if DOLLARS_IN_IDENTIFIERS
  992.   ++is_idchar['$'];
  993.   ++is_idstart['$'];
  994. #endif
  995.  
  996.   /* horizontal space table */
  997.   ++is_hor_space[' '];
  998.   ++is_hor_space['\t'];
  999. }
  1000.  
  1001. error (msg)
  1002. {
  1003.   printf ("error: %s\n", msg);
  1004. }
  1005.  
  1006. warning (msg)
  1007. {
  1008.   printf ("warning: %s\n", msg);
  1009. }
  1010.  
  1011. struct hashnode *
  1012. lookup (name, len, hash)
  1013.      char *name;
  1014.      int len;
  1015.      int hash;
  1016. {
  1017.   return (DEFAULT_SIGNED_CHAR) ? 0 : ((struct hashnode *) -1);
  1018. }
  1019. #endif
  1020.